<template>
	<view class="addcard">
		<view class="box">
			<view class="item">
				<view class="name">
					手机号
				</view>
				<u--input border="none" type="number" v-model="phone"></u--input>
			</view>
			<view class="item">
				<view class="name">
					验证码
				</view>
				<u-input border="none" v-model="code">
					<template slot="suffix">
						<u-button @tap="getCode" :text="tips" type="success" size="mini"></u-button>
					</template>
				</u-input>
			</view>
		</view>
		<view class="button_box">
			<view @click="loginbind" class="button act">
				确定
			</view>
		</view>
	</view>
</template>

<script>
	import {
		loginbind
	} from '@/api/mine'
	import {
		identifyingcodesms
	} from '@/api/login'
	export default {
		data() {
			return {
				pass: '',
				code: "",
				codeid: '',
				tips: "获取验证码",
				count: "60",
				timer: null,
				phone: '',
				openId: '',
				unionId:''
			}
		},
		onLoad(option) {
			this.openId = option.openId;
			this.unionId = option.unionId;
			// this.getinfo();
		},
		methods: {
			loginbind() {
				if (!(/^1[3456789]\d{9}$/.test(this.phone))) {
					if (!this.phone) {
						uni.showToast({
							title: '请输入手机号',
							duration: 2000,
							icon: 'none'
						});
					} else {
						uni.showToast({
							title: '手机号输入不正确',
							duration: 2000,
							icon: 'none'
						});
					}
					return
				}
				let data = {
					phone: this.phone,
					smsCodeId: this.codeid,
					smsCode: this.code,
					openId: this.openId,
					unionId:this.unionId
				}
				loginbind(data).then(res => {
					if (res.success == 1) {
						uni.showToast({
							icon: 'none',
							title: '绑定成功!'
						})
						// console.log(123,res.data)
						uni.setStorageSync('token', res.data.token)
						uni.setStorageSync('user', JSON.stringify(res.data))
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/index/index'
							});
						}, 1000)
					} else {
						uni.showToast({
							icon: 'none',
							title: res.message
						})
					}
				})
			},
			getCode() {
				if (!(/^1[3456789]\d{9}$/.test(this.phone))) {
					if (!this.phone) {
						uni.showToast({
							title: '请输入手机号',
							duration: 2000,
							icon: 'none'
						});
					} else {
						uni.showToast({
							title: '手机号输入不正确',
							duration: 2000,
							icon: 'none'
						});
					}
					return
				}
				let that = this
				const TIME_COUNT = 60;
				let data = {
					phone: this.phone
				}
				if (!this.timer) {
					identifyingcodesms(data).then(res => {
						if (res.success == 1) {
							this.codeid = res.data.id
							uni.showToast({
								icon: 'none',
								title: '验证码发送成功!'
							})
							that.count = TIME_COUNT;
							that.timer = setInterval(() => {
								if (that.count > 0 && that.count <= TIME_COUNT) {
									that.count--;
									that.tips = that.count + 's';
								} else {
									that.tips = '获取验证码';
									clearInterval(that.timer);
									that.timer = null;
								}
							}, 1000);
						} else {
							uni.showToast({
								title: res.message,
								duration: 2000,
								icon: 'none'
							});
						}
					})
				} else {
					uni.showToast({
						icon: 'none',
						title: '请稍后再获取验证码!'
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background: #e7e9ec;
	}

	.addcard {
		padding: 50rpx 0;
	}

	.box {
		padding: 30rpx;
		background: #FFFFFF;
		border-radius: 15px 15px 0px 0px;
		margin-bottom: 100rpx;

		.item {
			display: flex;
			align-items: center;
			padding: 25rpx 10rpx;
			border-bottom: 2rpx solid rgba(151, 151, 151, 0.2);

			.name {
				width: 25%;
				color: rgba(0, 0, 0, 0.9);
				font-size: 34rpx;
			}

			.right {
				width: 73%;
			}
		}

		.nobor {
			border: none;
		}
	}

	.button_box {
		text-align: center;
		padding-bottom: 80rpx;

		.button {
			display: inline-block;
			background: #FFBE53;
			border-radius: 20px;
			padding: 10rpx 80rpx;
		}

		.act {
			background: #FFBE53;
			border-radius: 40rpx;
			color: #FFFDEF;
		}

		.noact {
			border-radius: 40rpx;
			border: 2rpx solid #A6A6A6;
			color: #A6A6A6;
		}
	}
</style>
